// pages/shopManager/goodsManager/classifyManager/classifyManager.js
const common = require('../../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        classifyList: [],
        nowPage: 0,
        isLoading: false,
        isAll: false,
        addClassifyShow: false,
        isHome: true,
        classifyName: '',
        classifyBanner: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getClassifyList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.setData({
            classifyList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false,
            addClassifyShow: false,
            classifyName: ''
        })
        this.getClassifyList()
        setTimeout(function () {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.getClassifyList()
    },

    getClassifyList: function () {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        const that = this
        let classifyList = this.data.classifyList
        let data = {
            store_id: wx.getStorageSync('storeId'),
            type: 1,
            limit: 10,
            page: this.data.nowPage
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        common.post('Goodsclass/store_class_list', data).then(function (res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    classifyList: classifyList,
                    isAll: true
                })
            } else {
                classifyList = classifyList.concat(res)
                that.setData({
                    classifyList: classifyList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function (e) {})
    },

    goClassifyGoods: function (e) {
        wx.navigateTo({
            url: '/pages/shopManager/goodsManager/goodsManager/goodsManager?classifyId=' + e.currentTarget.dataset.id + '&type=normal'
        })
    },

    goClassifyEdit: function () {
        wx.navigateTo({
            url: '/pages/shopManager/goodsManager/classifyEdit/classifyEdit'
        })
    },

    goAddClassify: function () {
        this.setData({
            addClassifyShow: true
        })
    },

    myCatchTouch: function () {
        return
    },

    cancleClassifyShow: function () {
        this.setData({
            addClassifyShow: false
        })
    },

    classifyNameInput: function (e) {
        this.setData({
            classifyName: e.detail.value
        })
    },

    isHomeChange: function (e) {
        this.setData({
            isHome: e.detail.value
        })
    },

    uploadBannerImg: function () {
        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success(res) {
                const tempFilePaths = res.tempFilePaths[0];
                wx.navigateTo({
                    url: '/pages/cropper/cropper?type=4&target=classifyBanner&src=' + tempFilePaths + '&ratio=true&width=400&height=200'
                })
            }
        })
    },

    sureAddClassify: function () {
        if (this.data.classifyName == '' || this.data.classifyBanner == '') {
            wx.showToast({
                title: '请填写分类信息',
                icon: 'none',
                duration: 2000
            })
            return
        }
        wx.showLoading({
            title: '请稍后'
        })
        const that = this
        let data = {
            store_id: wx.getStorageSync('storeId'),
            class_name: this.data.classifyName,
            class_img: this.data.classifyBanner,
            show_type: this.data.isHome ? '4' : '1'
        }
        common.post('Goodsclass/store_add_class', data).then(function (res) {
            wx.hideLoading()
            if (res.code == 204) {
                wx.showModal({
                    title: '提示',
                    content: res.error,
                    showCancel: false,
                    success(res) {}
                })
                that.setData({
                    addClassifyShow: false,
                    addClassifyName: ''
                })
                return
            }
            wx.showModal({
                title: '提示',
                content: '成功新建分类',
                showCancel: false,
                success(res) {
                    if (res.confirm) {
                        that.setData({
                            classifyList: [],
                            nowPage: 0,
                            isLoading: false,
                            isAll: false,
                            addClassifyShow: false,
                            classifyName: '',
                            classifyBanner: ''
                        })
                        that.getClassifyList()
                    }
                }
            })
        }).catch(function (e) {})
    },

    updateData: function () {
        const that = this
        let data = {
            store_id: wx.getStorageSync('storeId'),
            type: 1,
            limit: this.data.nowPage * 10,
            page: 0
        }
        common.post('Goodsclass/store_class_list', data).then(function (res) {
            if (res.code == 204) {
                that.setData({
                    classifyList: []
                })
            } else {
                that.setData({
                    classifyList: res
                })
            }
        }).catch(function (e) {})
    }
})